//
//  UILabel+AutoLayout.m
//  trj
//
//  Created by 小小算盘 on 16/1/13.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "UILabel+AutoLayout.h"

@implementation UILabel (AutoLayout)
+(UILabel *)createLabelWithFrame:(CGRect)frame andTitle:(NSString *)title withColor:(UIColor *)color
{
    UILabel *label = [[UILabel alloc]initWithFrame:frame];
    label.text =title;
    label.textColor = color;
    CGRect autoFrame = label.frame;
    label.font = [UIFont systemFontOfSize:13];
    //自动折行设置
    label.numberOfLines = 0;
    label.frame = CGRectMake(frame.origin.x, frame.origin.y,frame.size.width,
                                autoFrame.size.height =[label.text boundingRectWithSize:
                                                          CGSizeMake(autoFrame.size.width, CGFLOAT_MAX)
                                                                                     options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                                                                  attributes:[NSDictionary dictionaryWithObjectsAndKeys:label.font,NSFontAttributeName, nil] context:nil].size.height);
    label.frame = CGRectMake(frame.origin.x, frame.origin.y,frame.size.width, autoFrame.size.height);
    return label;
}
- (CGSize)boundingRectWithSize:(CGSize)size{
    NSDictionary *attribute = @{NSFontAttributeName: self.font};
    
    CGSize retSize = [self.text boundingRectWithSize:size
                                             options:\
                      NSStringDrawingTruncatesLastVisibleLine |
                      NSStringDrawingUsesLineFragmentOrigin |
                      NSStringDrawingUsesFontLeading
                                          attributes:attribute
                                             context:nil].size;
    
    return retSize;
    
}

@end
